<!--
 * @Author: Lizhenyu
 * @Date: 2021-02-02 09:53:59
 * @FilePath: \usp-osm-frondend\src\views\serviceCenter\servicePackage\dialog\addService.vue
-->
<template>
  <div
    class="service-dialog"
    v-loading="pageLoading"
    element-loading-text="加载中..."
  >
    <el-dialog
      title="添加服务"
      :visible.sync="isShow"
      width="80%"
      element-loading-text="加载中..."
      :before-close="cancel"
      :append-to-body="true"
      v-if="isShow"
      top="0"
    >
      <div class="container-form">
        <el-row type="flex" justify="center">
          <el-col :span="24">
            <el-form
              :model="formData"
              label-position="left"
              label-width="80px"
              size="mini"
              ref="formData"
            >
              <el-row :gutter="60">
                <el-col :span="8">
                  <el-form-item label="服务编号">
                    <el-input
                      v-model="formData.code"
                      placeholder="服务编号"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="服务名称">
                    <el-input
                      v-model="formData.name"
                      placeholder="服务名称"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="服务类型" prop="type">
                    <el-select
                      v-model="formData.type"
                      placeholder="请选择"
                      popper-class="select-class"
                      :popper-append-to-body="false"
                    >
                      <el-option key label="全部" value></el-option>
                      <el-option
                        v-for="item in lookupItems.SERVICETYPE"
                        :key="item.dicCode"
                        :label="item.dicName"
                        :value="item.dicCode"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-collapse-transition>
                <div class="filterContent" v-if="showContent">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item label="创建时间">
                        <div class="block">
                            <el-date-picker
                              v-model="formData.createTime"
                              type="daterange"
                              range-separator="至"
                              :picker-options="pickerOptions"
                              :default-value="[new Date(), new Date()]"
                              value-format="yyyy-MM-dd"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                            ></el-date-picker>
                        </div>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </div>
              </el-collapse-transition>
            </el-form>
          </el-col>
        </el-row>
        <div style="padding-bottom: 20px">
          <el-row class="search-row">
            <el-col :span="8" :offset="16" style="text-align: right">
              <el-button
                @click="closeFilter"
                :icon="showContent ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                class="white-btn filterClose"
              >
                {{ showContent ? '折叠' : '展开' }}
              </el-button>
              <el-button
                @click="resetForm('formData')"
                icon="el-icon-refresh-left"
                class="white-btn"
              >
                重置
              </el-button>
              <el-button
                @click="searchFetchList"
                icon="el-icon-search"
                class="dark-btn search-btn"
                style="float: right"
              >
                {{ $t('search') }}
              </el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="container-table">
        <v-table
          :data="tableList"
          ref="table"
          :listLoading="listLoading"
          loadingText="加载中..."
          :paginationTotal="total"
          :page-size="formData.displayLength"
          @handleSelectionChange="handleSelectionChange"
          v-on:pageSizeChange="pageSizeChange"
          v-on:currentPageChange="currentPageChange"
          border
          pagination
          style="margin-top: 20px"
        >
          <el-table-column
            type="selection"
            :reserve-selection="true"
            width="55"
          >
          </el-table-column>
          <el-table-column
            label="服务编号"
            min-width="160"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.code }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="服务名称"
            min-width="125"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column
            label="服务商名称"
            min-width="160"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.serviceProviderName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="服务类型"
            min-width="100"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ getDictionaryNameByCode('SERVICETYPE',scope.row.type) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="是否绑定券池"
            min-width="100"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.isBoundCoupon === '1' ? '是' : '否' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
            min-width="130px"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.createTime | timestampToChange('time') }}
            </template>
          </el-table-column>
          <el-table-column
            prop="updateTime"
            label="更新时间"
            align="center"
            min-width="130px"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.updateTime | timestampToChange('time') }}
            </template>
          </el-table-column>
        </v-table>
      </div>
      <div class="footer">
        <div class="footer-btn">
          <el-button @click="cancel" class="white-btn">
            {{ $t('cancel') }}
          </el-button>
          <el-button
            @click="confirm"
            :loading="submitting"
            :disabled="disabled"
            class="dark-btn"
          >
            {{ $t('assetCenter.warehouseOrder.confirmation') }}
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import cloneDeep from 'lodash'
import VTable from '@/components/PageTable/index'
import { mapGetters } from 'vuex'
import { common } from '@/mixins/common'
import { getServiceList } from '@/api/service'
export default {
  name: 'addServicePackage',
  mixins: [common],
  components: {
    VTable
  },
  data () {
    return {
      formData: {
        code: '',
        createTime: [],
        isBoundCoupon: '',
        name: '',
        type: '',
        serviceProviderName: '',
        status: '2',
        displayLength: 10,
        displayStart: 0,
        ids: []
      },
      defaultTime: [new Date(), new Date()],
      tableList: [],
      selectList: [],
      disabledDate (time) {
        // 如果没有后面的-8.64e7就是不可以选择今天的
        return time.getTime() > Date.now() // - 8.64e7
      },
      showContent: false,
      total: 0,
      disabled: false,
      isShow: false,
      submitting: false,
      listLoading: false,
      pageLoading: false,
      pageType: '',
      typeArr: [],
      pickerOptions: {
        disabledDate (time) {
          // 如果没有后面的-8.64e7就是不可以选择今天的
          return time.getTime() > Date.now() // - 8.64e7
        }
      }
    }
  },
  computed: {
    ...mapGetters(['lookupItems'])
  },
  methods: {
    searchFetchList () {
      this.$nextTick(() => {
        this.$refs.table.restPage()
      })
      this.fetchList()
    },
    fetchList (idList) {
      this.listLoading = true
      const params = {
        ...this.formData,
        ids: idList,
        createTimeStart: this.formData.createTime ? new Date(this.formData.createTime[0] + ' ' + '00:00:00').getTime() : '',
        createTimeEnd: this.formData.createTime ? new Date(this.formData.createTime[1] + ' ' + '23:59:59').getTime() : ''
      }
      getServiceList(params).then(res => {
        if (res) {
          this.listLoading = false
          if (Number(res.code) === 1) {
            this.tableList = res.obj.data || []
            this.total = res.obj.total
          } else if (Number(res.code) === 0) {
            this.$alert(res.msg)
          } else {
            this.$alert(this.$t(res.code))
          }
        }
      })
    },
    open (idList, type, typeArr) {
      this.typeArr = typeArr
      this.isShow = true
      this.pageType = type
      this.fetchList(idList)
    },
    cancel () {
      this.isShow = false
      this.resetForm()
    },
    handleSelectionChange (val) {
      this.selectList = val
      if (this.selectList.length === 0) {
        this.disabled = true
      } else {
        this.disabled = false
      }
    },
    getRowKeys (row) {
      return row.id
    },
    isTypeSame (arr) {
      if (arr.length > 0) {
        const sameArr = []
        for (let i = 0; i < arr.length; i++) {
          let p = 0
          for (let j = 0; j < arr.length; j++) {
            if (arr[j] === arr[i]) {
              p++
            }
            if (p > 1 && sameArr.indexOf(arr[i]) === -1) {
              sameArr.push(arr[i])
            }
          }
        }
        if (sameArr.length > 0) {
          return false
        } else {
          return true
        }
      }
    },
    confirm () {
      this.isShow = false
      this.selectList.forEach(item => {
        this.typeArr.push(item.type)
      })
      if (this.isTypeSame(this.typeArr)) {
        this.$parent.getTableData(this.selectList)
      } else {
        this.$alert('服务包无法添加重复的服务类型，请重新选择')
      }
    },

    updateQty (stillNeedNum) {},
    // 修改page size
    pageSizeChange (size) {
      this.formData.displayLength = size
      this.fetchList()
    },

    // 修改current page
    currentPageChange (curPage) {
      this.formData.displayStart = curPage - 1
      this.fetchList()
    },

    resetForm () {
      this.formData = {
        code: '',
        createTime: [],
        isBoundCoupon: '',
        name: '',
        type: '',
        serviceProviderName: '',
        status: '2',
        displayLength: 10,
        displayStart: 0
      }
      this.submitting = false
      this.$refs.formData.resetFields()
      this.$nextTick(() => {
        this.$refs.table.restPage()
      })
      this.fetchList()
    },

    resetPage () {
      this.formData.displayStart = 0
      this.$refs.table.restPage()
    },

    closeFilter () {
      this.showContent = !this.showContent
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .el-form--label-top .el-form-item__label {
  padding-bottom: 0;
}
/deep/ .footer .footer-btn {
  padding-top: 0;
  margin: 30px 0 10px 0;
}
/deep/ .el-input__prefix {
  top: 3px;
}
</style>
